package com.hdu.hdu.chain;



import com.hdu.hdu.interceptor.Interceptor;
import com.hdu.hdu.interceptorFIlter.InterceptorFilter;
import com.hdu.hdu.invoker.Invoker;

import java.util.List;

public class InterceptorChain<T> {

    final InterceptorFilter INTERCEPTOR_FILTER;
    final List<Interceptor<T>> INTERCEPTORS;
    final Invoker<T> FINAL_INVOKER;

    public InterceptorChain(InterceptorFilter INTERCEPTOR_FILTER,
                            List<Interceptor<T>> INTERCEPTORS,
                            Invoker<T> FINAL_INVOKER) {
        this.INTERCEPTOR_FILTER = INTERCEPTOR_FILTER;
        this.INTERCEPTORS = INTERCEPTORS;
        this.FINAL_INVOKER = FINAL_INVOKER;
    }


    public void process(T context) {
        for (Interceptor<T> interceptor : INTERCEPTORS) {
            if (INTERCEPTOR_FILTER.needProcess(interceptor)) {
                interceptor.handle(context);
            }
        }
        FINAL_INVOKER.finalProcess(context);
    }
}
